package com.zeus.web.utils;

import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class Randomizer {
//	private static Randomizer instance = new Randomizer();
//	private SecureRandom secureRandom;
	private ThreadLocalRandom rand;

	public static Randomizer getInst() {
		return new Randomizer();
	}

	private Randomizer() {
//		secureRandom = new SecureRandom();
//		rand = new Random(secureRandom.nextLong());
		rand = ThreadLocalRandom.current(); 
	}

	public static int nextInt() {
		int ret = getInst().rand.nextInt();
		if (ret < 0) {
			ret = -ret;
		}
		return ret;
	}

	public static boolean randomOk(int seed, int ratio) {
		if (seed <= 0) {
			throw new IllegalArgumentException("the seed of random can't be zero");
		}
		return nextInt(seed) < ratio;
	}


	public static int nextInt(int min, int max) {
		if (max == min) {
			return min;
		}
		return min + nextInt(max - min);
	}

	public static int nextInt(int mod) {
		int ret = getInst().rand.nextInt(mod);
		return ret;
	}

	public static double nextDouble() {
		return getInst().rand.nextDouble();
	}

	public static Random getRandom() {
		return getInst().rand;
	}

	public static double nextGaussian() {
		return getInst().rand.nextGaussian();
	}
	
	/**
	 * 从list随机出一个元素
	 * 
	 * @param list
	 * @return
	 */
	public static <T> T randomList(List<T> list) {
		if (list == null || list.size() < 1) {
			throw new IllegalArgumentException("list is null or empty");
		}
		
		return list.get(nextInt(list.size()));
	}
}
